#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> v;
        if (root == nullptr)
            return v;
        stack<TreeNode*> st;
        TreeNode* pcur = root;
        st.push(pcur);
        while (st.size())
        {
            pcur = st.top();
            while (pcur->left && pcur->val != INT_MAX)
            {
                pcur = pcur->left;
                st.push(pcur);
            }
            if (pcur->val != INT_MAX)
                v.push_back(pcur->val);
            if (pcur->right)
            {
                st.pop();
                pcur = pcur->right;
                st.push(pcur);
            }
            else
            {
                st.pop();
                if (st.size())
                {
                    pcur = st.top();
                    v.push_back(pcur->val);
                    pcur->val = INT_MAX;
                }
            }
        }
        return v;
    }
};